////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRay_FireTurret extends Pawn
                     placeable;

var SkeletalMeshComponent BaseComponent;
var SkeletalMeshComponent WeaponComponent;
var BailterRay_TurretType FireTurret;
var CylinderComponent BaseCollision;
var CylinderComponent HeadCollision;
var float ElapsedTimeSinceFiring;
var bool bReadyToFire;
var name FiringSocket;
var rotator FireAim;
var bool bRotateClockWise;
var bool bWaiting;

const ROTATION_SPEED      = 5000;

simulated event PreBeginPlay()
{
    Super.PreBeginPlay();
    BaseCollision.SetTranslation(-(vector(BaseComponent.Rotation - rot(16384, 0 ,0)) * 40));
}


simulated function Tick(float DeltaTime)
{
    local vector FireLoc;
    local rotator FireRot;

    if(!PlayerCanSeeMe())
       return;

    ElapsedTimeSinceFiring += DeltaTime;

    if(!bWaiting)
    {
        if(bRotateClockWise)
        {
            if(FireAim.Pitch > -32768)
            {
                FireAim.Pitch -= DeltaTime * ROTATION_SPEED;

            }else
            {
                bWaiting = true;
                setTimer(0.4, false, 'wait');
            }
        }
        if(!bRotateClockWise)
        {
            if(FireAim.Pitch < 0)
            {
                FireAim.Pitch += DeltaTime * ROTATION_SPEED;

            }else
            {
                bWaiting = true;
                setTimer(0.4, false, 'wait');
            }
        }
    }

    //Update Weapon Direction
    WeaponComponent.SetRotation(FireAim);

    // Get location and direction of turret head
    WeaponComponent.GetSocketWorldLocationAndRotation(FiringSocket, FireLoc, FireRot);

    //Change collision box accordingly
    HeadCollision.SetTranslation(vector(WeaponComponent.Rotation) * 22);

    if(ElapsedTimeSinceFiring >= FireTurret.FireInterval)
    {
        Firing(FireLoc, FireRot);
        ElapsedTimeSinceFiring = 0;
    }
}

function wait()
{
    if(bRotateClockWise)
        bRotateClockWise = false;
    else
        bRotateClockWise = true;
    bWaiting = false;
}

function Firing(vector FireLoc, rotator FireRot)
{

    local UTEmitter MuzzleEmitter;
    local Projectile Proj;

    // Muzzle Flash Effect
    MuzzleEmitter = Spawn(class'UTEmitter', self,, FireLoc + (vector(FireRot) * 25), FireRot);
    MuzzleEmitter.SetTemplate(FireTurret.MuzzleFLash, true);
    MuzzleEmitter.SetDrawScale(2);
    FireTurret.FireSound.VolumeMultiplier = 0.2;
    FireTurret.FireSound.MaxAudibleDistance = 800;
    PlaySound(FireTurret.FireSound,,, true, self.location);

    // Spawns the Projectile
    Proj = Spawn(FireTurret.ProjectileClass, self,, FireLoc - (vector(FireRot) * 25), FireRot);
    Proj.SetDrawScale(0.8);
    Proj.Init(Normal(vector(FireRot)));

}

function SetMovementPhysics()
{

}

simulated State Dying
{
	simulated function BeginState(Name PreviousStateName)
	{
		super.BeginState(PreviousStateName);
		SetPhysics(PHYS_None);
		LifeSpan = 0.01;
		SetCollision(false);
		PlayDeathEffect();
	}

    // Do nothing when dead!
	function TakeDamage(int Damage, Controller EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser){}
	simulated function Tick(float DeltaTime){}


	simulated function PlayDeathEffect()
	{
	    local UTEmitter DeathEmitter;

        DeathEmitter = Spawn(class'UTEmitter', self,, self.Location, rot(0,16384,0));
	    DeathEmitter.SetTemplate(FireTurret.DeathEffect, true);
	    DeathEmitter.SetDrawScale(2);
	    DeathEmitter.PlaySound(FireTurret.DeathSound, false, true, false);
	}
}

function TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
    // So turrets dont destory themselves
    if(InstigatedBy.bIsPlayer == false)
    {
        return;
    }


	super.TakeDamage(Damage, InstigatedBy, HitLocation, Momentum, DamageType, HitInfo, DamageCauser);

    //Play hit effects based on hitlocation
    PlaySound(FireTurret.DeathSound, true,,, HitLocation);
    WorldInfo.MyEmitterPool.SpawnEmitter(FireTurret.DeathEffect, HitLocation, Rotator(HitLocation));

}

defaultproperties
{
    bRotateClockWise = true
    bWaiting = false

    //Health of turret
    Health=100

    //Clear any components from the parent class!
    Components.Empty

    //Base of Turret
    Begin Object class=SkeletalMeshComponent Name=BaseMesh
        SkeletalMesh=SkeletalMesh'BailterRayAssetPackage.Meshes.PlantTurretBase'
                CollideActors=True
        BlockActors=True
    End Object
    BaseComponent=BaseMesh
    Components.Add(BaseMesh)

    //Head of Turret
    Begin Object Class=SkeletalMeshComponent Name=HeadMesh
        SkeletalMesh=SkeletalMesh'BailterRayAssetPackage.Meshes.PlantTurretHead'
                CollideActors=True
        BlockActors=True
        Rotation=(Pitch = -16384, Yaw = 0, Roll = 0)
	End Object
	WeaponComponent=HeadMesh
	Components.Add(HeadMesh)

    //Turret Type
    Begin Object class=BailterRay_TurretType Name=Proj
		ProjectileClass=class'BailterRayProj_Flame'
		MuzzleFlash=ParticleSystem'BailterRayAssetPackage.Fire_Muzzle'
		DeathEffect=ParticleSystem'WP_BioRifle.Particles.P_WP_Bio_Alt_Blob_POP'
		DeathSound=SoundCue'A_Weapon_BioRifle.Weapon.A_BioRifle_FireAltImpactExplode_Cue'
		FireSound=SoundCue'A_Vehicle_Scorpion.SoundCues.A_Vehicle_Scorpion_EngineThrustLoop_Cue'
		FireInterval=.08
	End Object
	FireTurret=Proj

	//Collision
	Begin Object class=CylinderComponent name=CollisionBase
		CollisionRadius=15.0
		CollisionHeight=32.0
        CollideActors=True
        BlockActors=True
	End Object
    BaseCollision = CollisionBase
    Components.Add(CollisionBase)

   	Begin Object class=CylinderComponent name=CollisionHead
		CollisionRadius=14.0
		CollisionHeight=20.0
        CollideActors=True
        BlockActors=True
	End Object
    HeadCollision = CollisionHead
    Components.Add(CollisionHead)

    //Removing Default Collision
    Begin Object Name=CollisionCylinder
      CollisionHeight=1.0
      CollisionRadius=1.0
      CollideActors=True
      BlockActors=True
    End Object
    Components.Add(CollisionCylinder)
    CollisionComponent=CollisionCylinder
    CollisionType=COLLIDE_BlockAll

	FiringSocket = FiringMuzzle

    Components.Remove(Sprite)
	Name="Default__BailterRayFireTurret"
}